package com.ricky.sms.service.Impl;

import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.ricky.hosp.vo.sms.SmsVo;
import com.ricky.sms.service.SmsService;
import com.ricky.sms.util.ConstantPropertiesUtils;
import com.ricky.sms.util.PhoneFormatCheckUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@Service
public class SmsServiceImpl implements SmsService {
    @Override
    public boolean send(String phone, String code) {
        //判断手机号是否为空
        if(!PhoneFormatCheckUtils.isPhoneLegal(phone))
            return false;
        //整合cloopen短信服务
        //设置相关参数
        //生产环境请求地址：app.cloopen.com
        String serverIp = ConstantPropertiesUtils.SERVER_IP;
        //请求端口
        String serverPort = ConstantPropertiesUtils.SERVER_PORT;
        //主账号,登陆云通讯网站后,可在控制台首页看到开发者主账号ACCOUNT SID和主账号令牌AUTH TOKEN
        String accountSId = ConstantPropertiesUtils.ACCOUNT_SID;
        String accountToken = ConstantPropertiesUtils.ACCOUNT_TOKEN;
        //请使用管理控制台中已创建应用的APPID
        String appId = ConstantPropertiesUtils.APPID;
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(serverIp, serverPort);
        sdk.setAccount(accountSId, accountToken);
        sdk.setAppId(appId);
        sdk.setBodyType(BodyType.Type_JSON);
        String templateId= "1";
        String[] datas = {code,"5"};
//        String subAppend="1234";  //可选 扩展码，四位数字 0~9999
//        String reqId="fadfafas";  //可选 第三方自定义消息id，最大支持32位英文数字，同账号下同一自然天内不允许重复
        HashMap<String, Object> result = sdk.sendTemplateSMS(phone,templateId,datas);
//        HashMap<String, Object> result = sdk.sendTemplateSMS(to,templateId,datas,subAppend,reqId);
        if("000000".equals(result.get("statusCode"))){
            //正常返回输出data包体信息（map）
            HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
            Set<String> keySet = data.keySet();
            for(String key:keySet){
                Object object = data.get(key);
                System.out.println(key +" = "+object);
            }
            return true;
        }else{
            //异常返回输出错误码和错误信息
            System.out.println("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
            return false;
        }
    }
    // mq发送短信
    @Override
    public boolean send(SmsVo smsVo) {
        if (!StringUtils.isEmpty(smsVo.getPhone())){
//            boolean isSend = this.send(smsVo.getPhone(), smsVo.getParam());
            boolean isSend = true;
            System.out.println("已发送预约短信至："+smsVo.getPhone());
            return isSend;
        }
        return false;
    }


    private boolean send(String phone, Map<String,Object> param) {
        //判断手机号是否为空
        if(!PhoneFormatCheckUtils.isPhoneLegal(phone))
            return false;
        //整合cloopen短信服务
        //设置相关参数
        //生产环境请求地址：app.cloopen.com
        String serverIp = ConstantPropertiesUtils.SERVER_IP;
        //请求端口
        String serverPort = ConstantPropertiesUtils.SERVER_PORT;
        //主账号,登陆云通讯网站后,可在控制台首页看到开发者主账号ACCOUNT SID和主账号令牌AUTH TOKEN
        String accountSId = ConstantPropertiesUtils.ACCOUNT_SID;
        String accountToken = ConstantPropertiesUtils.ACCOUNT_TOKEN;
        //请使用管理控制台中已创建应用的APPID
        String appId = ConstantPropertiesUtils.APPID;
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(serverIp, serverPort);
        sdk.setAccount(accountSId, accountToken);
        sdk.setAppId(appId);
        sdk.setBodyType(BodyType.Type_JSON);
//        String templateId= "1";
        String[] datas = {(String)param.get("title"),(String)param.get("duration")};
//        String subAppend="1234";  //可选 扩展码，四位数字 0~9999
//        String reqId="fadfafas";  //可选 第三方自定义消息id，最大支持32位英文数字，同账号下同一自然天内不允许重复
        HashMap<String, Object> result = sdk.sendTemplateSMS(phone,(String)param.get("templateId"),datas);
//        HashMap<String, Object> result = sdk.sendTemplateSMS(to,templateId,datas,subAppend,reqId);
        if("000000".equals(result.get("statusCode"))){
            //正常返回输出data包体信息（map）
            HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
            Set<String> keySet = data.keySet();
            for(String key:keySet){
                Object object = data.get(key);
                System.out.println(key +" = "+object);
            }
            return true;
        }else{
            //异常返回输出错误码和错误信息
            System.out.println("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
            return false;
        }
    }
}
